package com.tledu.config;

import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class OpenBrowser implements CommandLineRunner{

    private Environment environment;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("应用已经准备就绪 ... 启动浏览器并自动加载指定的页面 ... ");
        try {
            String port = environment.getProperty("server.port");
            if(null == port){
                port = "8080";
            }
            String contextPath = environment.getProperty("server.servlet.context-path");
            //指定自己项目的路径
            Runtime.getRuntime().exec("cmd   /c   start   http://localhost:"+port+contextPath);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
